package com.stars.empoly.vmodel

import android.app.Application
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.BR
import com.stars.empoly.model.AreaBean
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.net.IResp
import kotlinx.coroutines.launch


class AreaSelectVModel(application: Application) : BRViewModel<BRRepository>(application) {

    var  id= "0"//默认市


    var item= MutableLiveData<AreaBean>()
    var isShow= MutableLiveData<Boolean>(false)


    @Bindable
    open var k: String? =""
        set(value) {
            field = value
            notifyPropertyChanged(BR.k)
        }


    fun toSearch(){

        if(TextUtils.isEmpty(k)){
            isShow.value=false
            return
        }

        viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.getSearchArea(k) }
            if (it.ok()) {
                if(null!=it.data){
                    isShow.value=true
                }
               item.value=it.data

            }

        }


    }

    override fun apiFunc(): suspend () -> IResp<*> {
        return { NetEngine.service.getArea(id) }
    }

}
